//
//  XGCategoryTableViewCell.m
//  BSBuDeJie
//
//  Created by monkey on 2019/1/4.
//  Copyright © 2019 itcast. All rights reserved.
//

#import <Masonry/Masonry.h>

#import "XGCategoryTableViewCell.h"

#import "XGCategoryModel.h"

@interface XGCategoryTableViewCell ()

/// 名称
@property (nonatomic,strong) UILabel *nameLabel;
/// 红色分割线
@property (nonatomic,strong) UIView *separatorRedView;

@end

@implementation XGCategoryTableViewCell

#pragma mark - 解析模型

- (void)setCategoryModel:(XGCategoryModel *)categoryModel
{
    _categoryModel = categoryModel;
    
    self.nameLabel.text = categoryModel.name;
}

#pragma mark - 构造方法

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

- (void)setUpUI
{
    // 添加子控件
    [self.contentView addSubview:self.separatorRedView];
    [self.contentView addSubview:self.nameLabel];
    
    // 设置自动布局
    [self.separatorRedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.contentView);
        make.width.mas_equalTo(5);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.separatorRedView.mas_right).offset(kMargin);
        make.centerY.equalTo(self.contentView);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    self.nameLabel.textColor = selected ? [UIColor redColor] : [UIColor lightGrayColor];
    self.separatorRedView.hidden = !selected;
}

#pragma mark - 懒加载

- (UILabel *)nameLabel
{
    if (_nameLabel == nil) {
        _nameLabel = [UILabel labelWithText:@"测试文本" textColor:[UIColor lightGrayColor] font:15 textAlignment:NSTextAlignmentCenter];
    }
    
    return _nameLabel;
}

- (UIView *)separatorRedView
{
    if (_separatorRedView == nil) {
        _separatorRedView = [[UIView alloc] init];
        _separatorRedView.backgroundColor = [UIColor redColor];
        _separatorRedView.hidden = YES;
    }
    
    return _separatorRedView;
}

@end
